package com.srmt.feishu.client.impl.services;

import com.lark.oapi.Client;
import com.lark.oapi.service.drive.v1.model.*;
import com.srmt.feishu.client.DriveService;
import com.srmt.feishu.client.util.ResponseValidator;

/**
 * 默认的云盘服务实现
 *
 * 委托给飞书SDK客户端，并使用ResponseValidator统一处理响应
 *
 * @author SRMT Development Team
 * @since 2.0.0
 * @version 6.0.0 添加统一响应处理
 */
class DefaultDriveServiceImpl implements DriveService {
    private final Client client;

    DefaultDriveServiceImpl(Client client) {
        this.client = client;
    }

    @Override
    public String copyFile(CopyFileReq req) throws Exception {
        CopyFileResp resp = client.drive().v1().file().copy(req);
        return ResponseValidator.validateAndExtract(
                resp,
                "copyFile",
                r -> r.getData().getFile().getToken()
        );
    }

    @Override
    public void deleteFile(DeleteFileReq req) throws Exception {
        DeleteFileResp resp = client.drive().v1().file().delete(req);
        ResponseValidator.validateVoidResponse(resp, "deleteFile");
    }

    @Override
    public BaseMember createPermissionMember(CreatePermissionMemberReq req) throws Exception {
        CreatePermissionMemberResp resp = client.drive().v1().permissionMember().create(req);
        return ResponseValidator.validateAndExtract(
                resp,
                "createPermissionMember",
                r -> r.getData().getMember()
        );
    }

    @Override
    public BaseMember updatePermissionMember(UpdatePermissionMemberReq req) throws Exception {
        UpdatePermissionMemberResp resp = client.drive().v1().permissionMember().update(req);
        return ResponseValidator.validateAndExtract(
                resp,
                "updatePermissionMember",
                r -> r.getData().getMember()
        );
    }

    @Override
    public void deletePermissionMember(DeletePermissionMemberReq req) throws Exception {
        DeletePermissionMemberResp resp = client.drive().v1().permissionMember().delete(req);
        ResponseValidator.validateVoidResponse(resp, "deletePermissionMember");
    }

    @Override
    public Member[] listPermissionMember(ListPermissionMemberReq req) throws Exception {
        ListPermissionMemberResp resp = client.drive().v1().permissionMember().list(req);
        Member[] members = ResponseValidator.validateAndExtract(
                resp,
                "listPermissionMember",
                r -> r.getData().getItems()
        );
        return members;
    }
}
